package cc.geguol.iodemo.randomaccessdemo;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 作者：13687
 * 日期：2017/6/18
 * 项目：IODemo
 * 描述：只能操作文件
 * 需求：开三个线程去复制一个文件
 * 1. 需要知道文件的大小
 * 2. 需要知道每个文件的读写位置
 * 3. 所有线程读写完成后需要有一个结果
 */
public class RandomAccesssDemo {
	public static void main(String[] args) throws IOException, InterruptedException {
		int startpos = 0;
		ExecutorService threadPool = Executors.newCachedThreadPool();
		CountDownLatch countDownLatch = new CountDownLatch(3);//已知3个线程
		for (int i = 1; i <=3 ; i++) {
			FileInputStream fileInputStream = new FileInputStream("./files/text"+i+".txt");
			RandomAccessFile randomAccessFile = new RandomAccessFile("./files/rafmerge.txt","rw");
//			FileInputStream fileInputStream = new FileInputStream("./files/"+i+".temp");
//			RandomAccessFile randomAccessFile = new RandomAccessFile("./files/rafmerge.png","rw");
			randomAccessFile.seek(startpos);
//			创建work
			threadPool.execute(new WriteAble(countDownLatch,fileInputStream,randomAccessFile));
			startpos+=fileInputStream.available();
		}
		countDownLatch.await();
		System.out.println("文件应该合并完成了");
		threadPool.shutdownNow();

	}
}
